}
+/* Get the frame rect, in system coordinates, of the parent window or,
+ if there is no parent window, the main screen. */
+static inline NSRect
+ns_parent_window_rect (struct frame *f)
+{
+ NSRect parentRect;
+
+ if (FRAME_PARENT_FRAME (f) != NULL)
+ {
+ EmacsView *parentView = FRAME_NS_VIEW (FRAME_PARENT_FRAME (f));
+ parentRect = [parentView convertRect:[parentView frame]
+ toView:nil];
+ parentRect = [[parentView window] convertRectToScreen:parentRect];
+ }
+ else
+ parentRect = [[[NSScreen screens] objectAtIndex:0] frame];
+
+ return parentRect;
+}
+
+/* Calculate system coordinates of the left and top of the parent
+ window or, if there is no parent window, the main screen. */
+#define NS_PARENT_WINDOW_LEFT_POS(f) NSMinX (ns_parent_window_rect (f))
+#define NS_PARENT_WINDOW_TOP_POS(f) NSMaxY (ns_parent_window_rect (f))
+
+
static NSRect
ns_row_rect (struct window *w, struct glyph_row *row,
enum glyph_row_area area)
-------------------------------------------------------------------------- */
{
NSView *view = FRAME_NS_VIEW (f);
- NSScreen *screen = [[view window] screen];
+ NSRect windowFrame = [[view window] frame];
+ NSPoint topLeft;
NSTRACE ("ns_set_offset");
block_input ();
- f->left_pos = xoff;
- f->top_pos = yoff;
+ if (FRAME_PARENT_FRAME (f))
+ {
+ /* Convert the parent frame's view rectangle into screen
+ coords. */
+ EmacsView *parentView = FRAME_NS_VIEW (FRAME_PARENT_FRAME (f));
+ NSRect parentRect = [parentView convertRect:[parentView frame]
+ toView:nil];
+ parentRect = [[parentView window] convertRectToScreen:parentRect];
+
+ if (f->size_hint_flags & XNegative)
+ topLeft.x = NSMaxX (parentRect) - NSWidth (windowFrame) + xoff;
+ else
+ topLeft.x = NSMinX (parentRect) + xoff;
- if (view != nil)
+ if (f->size_hint_flags & YNegative)
+ topLeft.y = NSMinY (parentRect) + NSHeight (windowFrame) - yoff;
+ else
+ topLeft.y = NSMaxY (parentRect) - yoff;
+ }
+ else
{
- if (FRAME_PARENT_FRAME (f) == NULL && screen)
- {
- f->left_pos = f->size_hint_flags & XNegative
- ? [screen visibleFrame].size.width + f->left_pos - FRAME_PIXEL_WIDTH (f)
- : f->left_pos;
- /* We use visibleFrame here to take menu bar into account.
- Ideally we should also adjust left/top with visibleFrame.origin. */
-
- f->top_pos = f->size_hint_flags & YNegative
- ? ([screen visibleFrame].size.height + f->top_pos
- - FRAME_PIXEL_HEIGHT (f) - FRAME_NS_TITLEBAR_HEIGHT (f)
- - FRAME_TOOLBAR_HEIGHT (f))
- : f->top_pos;
-#ifdef NS_IMPL_GNUSTEP
- if (f->left_pos < 100)
- f->left_pos = 100; /* don't overlap menu */
-#endif
- }
- else if (FRAME_PARENT_FRAME (f) != NULL)
- {
- struct frame *parent = FRAME_PARENT_FRAME (f);
+ /* If there is no parent frame then just convert to screen
+ coordinates, UNLESS we have negative values, in which case I
+ think it's best to position from the bottom and right of the
+ current screen rather than the main screen or whole
+ display. */
+ NSRect screenFrame = [[[view window] screen] frame];
- /* On X negative values for child frames always result in
- positioning relative to the bottom right corner of the
- parent frame. */
- if (f->left_pos < 0)
- f->left_pos = FRAME_PIXEL_WIDTH (parent) - FRAME_PIXEL_WIDTH (f) + f->left_pos;
+ if (f->size_hint_flags & XNegative)
+ topLeft.x = NSMaxX (screenFrame) - NSWidth (windowFrame) + xoff;
+ else
+ topLeft.x = xoff;
- if (f->top_pos < 0)
- f->top_pos = FRAME_PIXEL_HEIGHT (parent) + FRAME_TOOLBAR_HEIGHT (parent)
- - FRAME_PIXEL_HEIGHT (f) + f->top_pos;
- }
+ if (f->size_hint_flags & YNegative)
+ topLeft.y = NSMinY (screenFrame) + NSHeight (windowFrame) - yoff;
+ else
+ topLeft.y = NSMaxY ([[[NSScreen screens] objectAtIndex:0] frame]) - yoff;
+
+#ifdef NS_IMPL_GNUSTEP
+ /* Don't overlap the menu.
- /* Constrain the setFrameTopLeftPoint so we don't move behind the
- menu bar. */
- NSPoint pt = NSMakePoint (SCREENMAXBOUND (f->left_pos
- + NS_PARENT_WINDOW_LEFT_POS (f)),
- SCREENMAXBOUND (NS_PARENT_WINDOW_TOP_POS (f)
- - f->top_pos));
- NSTRACE_POINT ("setFrameTopLeftPoint", pt);
- [[view window] setFrameTopLeftPoint: pt];
- f->size_hint_flags &= ~(XNegative|YNegative);
+ FIXME: Surely there's a better way than just hardcoding 100
+ in here? */
+ boundsRect.origin.x = 100;
+#endif
}
+ NSTRACE_POINT ("setFrameTopLeftPoint", topLeft);
+ [[view window] setFrameTopLeftPoint:topLeft];
+ f->size_hint_flags &= ~(XNegative|YNegative);
+
unblock_input ();
}
make_fixnum (FRAME_NS_TITLEBAR_HEIGHT (f)),
make_fixnum (FRAME_TOOLBAR_HEIGHT (f))));
- [window setFrame: wr display: YES];
+ /* Usually it seems safe to delay changing the frame size, but when a
+ series of actions are taken with no redisplay between them then we
+ can end up using old values so don't delay here. */
+ change_frame_size (f,
+ FRAME_PIXEL_TO_TEXT_WIDTH (f, pixelwidth),
+ FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelheight),
+ 0, NO, 0, 1);
+
+ [window setFrame:wr display:NO];
- [view updateFrameSize: NO];
unblock_input ();
}
so some key presses (TAB) are swallowed by the system. */
[window makeFirstResponder: view];
- [view updateFrameSize: NO];
unblock_input ();
}
}
if ([view judge])
removed = YES;
}
-
- if (removed)
- [eview updateFrameSize: NO];
}
/* ==========================================================================
- (void)dealloc
{
NSTRACE ("[EmacsView dealloc]");
+
+ /* Clear the view resize notification. */
+ [[NSNotificationCenter defaultCenter]
+ removeObserver:self
+ name:NSViewFrameDidChangeNotification
+ object:nil];
+
+ CGContextRelease (drawingBuffer);
+
[toolbar release];
if (fs_state == FULLSCREEN_BOTH)
[nonfs_window release];
return NO;
}
-- (void) updateFrameSize: (BOOL) delay
-{
- NSWindow *window = [self window];
- NSRect wr = [window frame];
- int extra = 0;
- int oldc = cols, oldr = rows;
- int oldw = FRAME_PIXEL_WIDTH (emacsframe);
- int oldh = FRAME_PIXEL_HEIGHT (emacsframe);
- int neww, newh;
-
- NSTRACE ("[EmacsView updateFrameSize:]");
- NSTRACE_SIZE ("Original size", NSMakeSize (oldw, oldh));
- NSTRACE_RECT ("Original frame", wr);
- NSTRACE_MSG ("Original columns: %d", cols);
- NSTRACE_MSG ("Original rows: %d", rows);
-
- if (! [self isFullscreen])
- {
- int toolbar_height;
-#ifdef NS_IMPL_GNUSTEP
- // GNUstep does not always update the tool bar height. Force it.
- if (toolbar && [toolbar isVisible])
- update_frame_tool_bar (emacsframe);
-#endif
-
- toolbar_height = FRAME_TOOLBAR_HEIGHT (emacsframe);
- if (toolbar_height < 0)
- toolbar_height = 35;
-
- extra = FRAME_NS_TITLEBAR_HEIGHT (emacsframe)
- + toolbar_height;
- }
-
- if (wait_for_tool_bar)
- {
- /* The toolbar height is always 0 in fullscreen and undecorated
- frames, so don't wait for it to become available. */
- if (FRAME_TOOLBAR_HEIGHT (emacsframe) == 0
- && FRAME_UNDECORATED (emacsframe) == false
- && ! [self isFullscreen])
- {
- NSTRACE_MSG ("Waiting for toolbar");
- return;
- }
- wait_for_tool_bar = NO;
- }
-
- neww = (int)wr.size.width - emacsframe->border_width;
- newh = (int)wr.size.height - extra;
-
- NSTRACE_SIZE ("New size", NSMakeSize (neww, newh));
- NSTRACE_MSG ("FRAME_TOOLBAR_HEIGHT: %d", FRAME_TOOLBAR_HEIGHT (emacsframe));
- NSTRACE_MSG ("FRAME_NS_TITLEBAR_HEIGHT: %d", FRAME_NS_TITLEBAR_HEIGHT (emacsframe));
-
- cols = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (emacsframe, neww);
- rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (emacsframe, newh);
-
- if (cols < MINWIDTH)
- cols = MINWIDTH;
-
- if (rows < MINHEIGHT)
- rows = MINHEIGHT;
-
- NSTRACE_MSG ("New columns: %d", cols);
- NSTRACE_MSG ("New rows: %d", rows);
-
- if (oldr != rows || oldc != cols || neww != oldw || newh != oldh)
- {
- NSView *view = FRAME_NS_VIEW (emacsframe);
-
- change_frame_size (emacsframe,
- FRAME_PIXEL_TO_TEXT_WIDTH (emacsframe, neww),
- FRAME_PIXEL_TO_TEXT_HEIGHT (emacsframe, newh),
- 0, delay, 0, 1);
- SET_FRAME_GARBAGED (emacsframe);
- cancel_mouse_face (emacsframe);
-
- /* The next two lines set the frame to the same size as we've
- already set above. We need to do this when we switch back
- from non-native fullscreen, in other circumstances it appears
- to be a noop. (bug#28872) */
- wr = NSMakeRect (0, 0, neww, newh);
- [view setFrame: wr];
-#ifdef NS_DRAW_TO_BUFFER
- [self createDrawingBuffer];
-#endif
-
- // To do: consider using [NSNotificationCenter postNotificationName:].
- [self windowDidMove: // Update top/left.
- [NSNotification notificationWithName:NSWindowDidMoveNotification
- object:[view window]]];
- }
- else
- {
- NSTRACE_MSG ("No change");
- }
-}
- (NSSize)windowWillResize: (NSWindow *)sender toSize: (NSSize)frameSize
/* Normalize frame to gridded text size. */
{
int extra = 0;
+ int cols, rows;
NSTRACE ("[EmacsView windowWillResize:toSize: " NSTRACE_FMT_SIZE "]",
NSTRACE_ARG_SIZE (frameSize));
sz = [self windowWillResize: theWindow toSize: sz];
#endif /* NS_IMPL_GNUSTEP */
- if (cols > 0 && rows > 0)
- {
- [self updateFrameSize: YES];
- }
-
ns_send_appdefined (-1);
}
#endif /* NS_IMPL_COCOA */
+- (void)viewDidResize:(NSNotification *)notification
+{
+ NSRect frame = [self frame];
+ int oldw, oldh, neww, newh;
+
+ if (! FRAME_LIVE_P (emacsframe))
+ return;
+
+#ifdef NS_DRAW_TO_BUFFER
+ CGFloat scale = [[self window] backingScaleFactor];
+ oldw = (CGFloat)CGBitmapContextGetWidth (drawingBuffer) / scale;
+ oldh = (CGFloat)CGBitmapContextGetHeight (drawingBuffer) / scale;
+#else
+ oldw = FRAME_PIXEL_WIDTH (emacsframe);
+ oldh = FRAME_PIXEL_HEIGHT (emacsframe);
+#endif
+ neww = (int)NSWidth (frame);
+ newh = (int)NSHeight (frame);
+
+ NSTRACE ("[EmacsView viewDidResize]");
+
+ /* Don't want to do anything when the view size hasn't changed. */
+ if ((oldh == newh && oldw == neww))
+ {
+ NSTRACE_MSG ("No change");
+ return;
+ }
+
+ NSTRACE_SIZE ("Original size", NSMakeSize (oldw, oldh));
+ NSTRACE_SIZE ("New size", NSMakeSize (neww, newh));
+
+ change_frame_size (emacsframe,
+ FRAME_PIXEL_TO_TEXT_WIDTH (emacsframe, neww),
+ FRAME_PIXEL_TO_TEXT_HEIGHT (emacsframe, newh),
+ 0, YES, 0, 1);
+
+#ifdef NS_DRAW_TO_BUFFER
+ [self createDrawingBuffer];
+#endif
+ SET_FRAME_GARBAGED (emacsframe);
+ cancel_mouse_face (emacsframe);
+}
+
+
- (void)windowDidBecomeKey: (NSNotification *)notification
/* cf. x_detect_focus_change(), x_focus_changed(), x_new_focus_frame() */
{
maximizing_resize = NO;
#endif
-#ifdef NS_DRAW_TO_BUFFER
- [self createDrawingBuffer];
-#endif
-
win = [[EmacsWindow alloc]
initWithContentRect: r
styleMask: (FRAME_UNDECORATED (f)
[NSApp registerServicesMenuSendTypes: ns_send_types
returnTypes: [NSArray array]];
+#ifdef NS_DRAW_TO_BUFFER
+ [self createDrawingBuffer];
+#endif
+
+ /* Set up view resize notifications. */
+ [self setPostsFrameChangedNotifications:YES];
+ [[NSNotificationCenter defaultCenter]
+ addObserver:self
+ selector:@selector (viewDidResize:)
+ name:NSViewFrameDidChangeNotification object:nil];
+
/* macOS Sierra automatically enables tabbed windows. We can't
allow this to be enabled until it's available on a Free system.
Currently it only happens by accident and is buggy anyway. */
return;
if (screen != nil)
{
- emacsframe->left_pos = r.origin.x - NS_PARENT_WINDOW_LEFT_POS (emacsframe);
- emacsframe->top_pos =
- NS_PARENT_WINDOW_TOP_POS (emacsframe) - (r.origin.y + r.size.height);
+ emacsframe->left_pos = NSMinX (r) - NS_PARENT_WINDOW_LEFT_POS (emacsframe);
+ emacsframe->top_pos = NS_PARENT_WINDOW_TOP_POS (emacsframe) - NSMaxY (r);
// FIXME: after event part below didExitFullScreen is not received
// if (emacs_event)
{
[toolbar setVisible:YES];
update_frame_tool_bar (emacsframe);
- [self updateFrameSize:YES];
[[self window] display];
}
else
// send notifications.
[self windowWillExitFullScreen];
- [fw setFrame: [w frame] display:YES animate:ns_use_fullscreen_animation];
+ [fw setFrame:[[w contentView] frame]
+ display:YES animate:ns_use_fullscreen_animation];
[fw close];
[w makeKeyAndOrderFront:NSApp];
[self windowDidExitFullScreen];
- [self updateFrameSize:YES];
}
}
}
-- (void) setRows: (int) r andColumns: (int) c
-{
- NSTRACE ("[EmacsView setRows:%d andColumns:%d]", r, c);
- rows = r;
- cols = c;
-}
-
- (int) fullscreenState
{
return fs_state;